package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.MConst
import com.cn.qgx428.lazydiary.db.bean.CStikyNote
import com.cn.qgx428.lazydiary.db.dao.StikyNoteDao
import com.cn.qgx428.lazydiary.dialog.ImgAlertDialog
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity

import kotlinx.android.synthetic.main.activity_stick_note_change.*
import org.jetbrains.anko.toast

class StickNoteChangeActivity : BaseActivity() {

    lateinit var stickNote: CStikyNote

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stick_note_change)

        stickNote = intent.extras.getSerializable(MConst.S_STICK_NOTE) as CStikyNote
        initView()
    }

    private fun initView() {
        stickNoteChange_deleteBtn.setOnClickListener({
            ImgAlertDialog(this){
                val dao = StikyNoteDao()
                val id = dao.delete(stickNote.id)
                if (id == 1){
                    toast("已删除")
                    LManager.newInstance().trigger(LAction.DB_DELETE_STICK_NOTE)
                    finish()
                }
            }.show()
        })

        stickNoteChange_back.setOnClickListener({ finish() })
        stickNoteChange_editBtn.setOnClickListener({ stickNoteChange_input.isEnabled = true})

        stickNoteChange_time.setText(stickNote.createTime)
        stickNoteChange_input.setText(stickNote.content)

        if (TextUtils.isEmpty(stickNote.noticeTime)){
            stickNoteChange_promptLayout.visibility = View.GONE
        } else{
            stickNoteChange_promptLayout.visibility = View.VISIBLE
            var timeString = stickNote.noticeTime
            val index = stickNote.noticeTime?.indexOf("年")
            stickNoteChange_promptTxt.text = timeString?.substring(index!! + 1)
        }

        stickNoteChange_saveBtn.setOnClickListener({
            stickNote.content = stickNoteChange_input.text.toString()
            val dao = StikyNoteDao()
            val id = dao.update(stickNote)
            if(id == 1){
                toast("已更新")
                LManager.newInstance().trigger(LAction.DB_UPDATE_BILL)
                finish()
            }
        })

    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }
}
